/*
 * @filename SecurityUser.java
 * @author barry
 * @version 0.0.1
 * @date 2020年1月3日
 */
package com.bnzj.core.web.security;

import java.util.Collection;

import javax.validation.constraints.NotNull;

import com.bnzj.core.rest.UserInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author barry
 * @date 2020-01-03
 */
public class SecurityUser implements UserDetails, UserInfo {
    private static final long serialVersionUID = 6000680762935141148L;

    private transient volatile Collection<? extends GrantedAuthority> authorities = null;

    private final SecurityResourceManager securityResourceManager;

    private final String username;

    private String cname;

    private String email;

    private String password;

    private boolean enabled;

    private boolean expired;

    public SecurityUser(@NotNull SecurityResourceManager securityResourceManager, @NotNull String username) {
        this.securityResourceManager = securityResourceManager;
        this.username = username;
    }

    public SecurityUser(@NotNull SecurityResourceManager securityManager, @NotNull String username, String cname, String email, String password, boolean enabled, boolean expired) {
        this(securityManager, username);
        this.cname = cname;
        this.email = email;
        this.password = password;
        this.enabled = enabled;
        this.expired = expired;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (this.authorities == null || this.authorities.isEmpty()) {
            synchronized (this) {
                if (this.authorities == null || this.authorities.isEmpty()) {
                    this.authorities = securityResourceManager.getAuthorities(username);
                }
            }
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return expired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setExpired(boolean expired) {
        this.expired = expired;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }


    public static void main(String[] args){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);
        System.out.println(encoder.encode("123456"));
    }

    @Override
    public String getAccount() {
        return username;
    }

    @Override
    public String getName() {
        return cname;
    }

    @Override
    public String getEmail() {
        return email;
    }

}
